<?php
/**
 * Created by PhpStorm.
 * User: junyv
 * Date: 2017/4/5
 * Time: 15:49
 */

namespace app\home\controller;


class Map extends Base
{
    public function _initialize(){
        parent::_initialize();
        $this->assign('ak','ETLXgCxIoVixggHcAk6mKpMd');
    }
    public function index(){
        $id = input('param.house_id/d',0);
        $type = input('param.type','house');
        $this->getAttr($type);
        $this->assign('listsurl',url($this->getModel($type).'/index'));
        if($id){
            return $this->getInfo($id,$type);
        }
        if($type=='second_house'){
            return $this->fetch('map/second');
        }
        if($type == 'rental'){
            return $this->fetch('map/rental');
        }
        return $this->fetch();
    }
    private function getInfo($id,$type){
        $where['id'] = $id;
        $info = model($type)->where($where)->find()->toArray();
        if($type!='second_house' && $type!='rental'){
            $data = model($type.'_data')->where($where)->find()->toArray();
            $info = array_merge($info,$data);
        }
        $this->setSeo($info);
        $this->assign('chaoxiang',cache('linkmenu_12')['cate']);
        $this->assign('zhuangxiu',cache('linkmenu_9')['cate']);
        $this->assign('info',$info);
        return $this->fetch('map/'.$type.'_detail');
    }
    //得 到城市区域
    public function ajaxGetArea(){
        $city = cache('city');
        $city = $city['tree'];
        $pid  = input('param.pid/d',0);
        $return['status'] = 0;
        if(isset($city[$pid]) && isset($city[$pid]['_child'])){
            $return['status'] = 1;
            $return['data'] = $city[$pid]['_child'];
        }else{
            $return['data'] = [];
        }
        return json($return);
    }
    public function getHouseList(){
        $area = input('get.area/d',0);
        $room = input('get.room/d',0);
        $tags = input('get.tags/d',0);
        $price = input('get.price/d',0);
        $renovation = input('get.renovation/d',0);
        $house_type = input('get.house_type/d',0);
        $keyword = input('get.keyword');
        $where['status'] = 1;
        $where['city'] = $this->cityInfo['id'];
        $price && $where['average_price'] = getHousePrice($price);
        $renovation && $where['zhuangxiu_id'] = $renovation;
        $house_type && $where[] = ['exp',"find_in_set({$house_type},house_type)"];
        $area && $where['area'] = $area;
        $room && $where[] = ['exp',"find_in_set({$room},room_type)"];
        $tags && $where[] = ['exp',"find_in_set({$tags},tags_id)"];
        $keyword && $where['title'] = ['like','%'.$keyword.'%'];
        $lists = model('house')->where($where)->field('id,title,img,lat,city_name,area_name,tags_name,lng,house_status,group_youhui,youhui,average_price,address,sale_phone')->order($this->getSort())->select();
        if($lists){
            $return['status'] = 1;
            $return['data']   = $lists;
            $return['total']  = count($lists);
        }else{
            $return['status'] = 0;
            $return['data']   = '';
            $return['total']  = 0;
        }
        return json($return);
    }
    public function getSecondList(){
        $area = input('get.area/d',0);
        $mianji = input('get.mianji/d',0);
        $price = input('get.price/d',0);
        $keyword = input('get.keyword');
        $where['status'] = 1;
        $where['city'] = $this->cityInfo['id'];
        $area && $where['area'] = $area;
        $mianji && $where['mianji'] = action('Second/getMianji',['key'=>$mianji]);
        $price && $where['total_price'] = action('Second/getPrice',['key'=>$price]);
        $keyword && $where['title'] = ['like','%'.$keyword.'%'];
        $lists = model('second_house')->where($where)->field('id,title,img,lat,lng,city_name,area_name,total_price,price,address,contact,contact_phone')->order($this->getSort())->select();
        if($lists){
            $return['status'] = 1;
            $return['data']   = $lists;
            $return['total']  = count($lists);
        }else{
            $return['status'] = 0;
            $return['data']   = '';
            $return['total']  = 0;
        }
        return json($return);
    }
    public function getRentalList(){
        $area = input('get.area/d',0);
        $mianji = input('get.mianji/d',0);
        $price = input('get.price/d',0);
        $keyword = input('get.keyword');
        $where['status'] = 1;
        $where['city'] = $this->cityInfo['id'];
        $area && $where['area'] = $area;
        $mianji && $where['mianji'] = action('Chuzu/getMianji',['key'=>$mianji]);
        $price && $where['price'] = action('Chuzu/getPrice',['key'=>$price]);
        $keyword && $where['title'] = ['like','%'.$keyword.'%'];
        $lists = model('rental')->where($where)->field('id,title,img,lat,lng,city_name,area_name,yajin,price,address,contact,contact_phone')->order($this->getSort())->select();
        if($lists){
            $return['status'] = 1;
            $return['data']   = $lists;
            $return['total']  = count($lists);
        }else{
            $return['status'] = 0;
            $return['data']   = '';
            $return['total']  = 0;
        }
        return json($return);
    }
    private function getSort(){
        $sort = input('get.sort/d',1);
        switch($sort){
            case 1:
                $str = 'id desc';
                break;
            case 2:
                $str = 'hits desc';
                break;
            case 3:
                $str = 'average_price asc';
                break;
            case 4:
                $str = 'average_price desc';
                break;
            default:
                $str = 'ordid asc,id desc';
                break;

        }
        return $str;
    }
    private function getModel($type){
        switch($type){
            case 'house':
                $model = 'Loupan';
                $info['title'] = '地图找房';
                break;
            case 'second_house':
                $model = 'Second';
                $info['title'] = '地图找二手房源';
                break;
            case 'estate':
                $model = 'Xiaoqu';
                $info['title'] = '地图找小区';
                break;
            case 'rental':
                $model = 'Chuzu';
                $info['title'] = '地图找出租房';
                break;
            default:
                $model = 'Loupan';
                $info['title'] = '地图找房';
                break;
        }
        $this->setSeo($info);
        return $model;
    }
    private function getAttr($type){
        if($type=='house'){
            $room     = cache('linkmenu_6');//户型类型
            $tags     = cache('linkmenu_5');//特色标签
            $renovation = cache('linkmenu_9');//装修程度
            $house_type     = cache('linkmenu_7');//楼盘类型
            $this->assign('room',$room['cate']);
            $this->assign('tags',$tags['cate']);
            $this->assign('renovation',$renovation['cate']);
            $this->assign('price',getHousePrice());
            $this->assign('house_type',$house_type['cate']);
        }elseif($type=='rental'){
            $price = action('Chuzu/getPrice');
            $mianji = action('Chuzu/getMianji');
            $this->assign('price',$price);
            $this->assign('mianji',$mianji);
        }else{
            $price = action('Second/getPrice');
            $mianji = action('Second/getMianji');
            $this->assign('price',$price);
            $this->assign('mianji',$mianji);
        }
    }

}